<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ez="http://java.sun.com/jsf/composite/bill"
          
      xmlns:ezc="http://java.sun.com/jsf/composite/ezcomp">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>


        <ezc:test1compo/>


        <h:outputStylesheet library="css" name="printing.css" />

        <h:panelGroup rendered="#{!webUserController.hasPrivilege('OpdBilling')}" >
            You are NOT authorized
        </h:panelGroup>

        <h:panelGroup rendered="#{webUserController.hasPrivilege('OpdBilling')}" styleClass="opdPanel" >
            <h:form id="form">
                <p:focus id="focusIx" for="acIx" />
                <h:panelGroup rendered="#{!billController.printPreview}" styleClass="alignTop" >
                    <p:panel style="min-height: 600px; position:relative;"  >

                        <h:panelGrid columns="20"  >
                            <h:outputLabel value="OPD Bill" />
                            <h:outputLabel value="&nbsp;&nbsp;&nbsp;&nbsp;" style="min-width: 50px;" />
                            <p:selectOneMenu   id="cmbPs" value="#{billController.paymentScheme}">                                                                     
                                <f:selectItems value="#{paymentSchemeController.items}" var="paysch" itemLabel="#{paysch.name}" itemValue="#{paysch}"  />
                                <p:ajax process="cmbPs wsd wcd" update="pBillDetails  tblBillItem lblCardRef txtCardRef creditBank lblCheqRef txtCheqRef chequeDate bank lblSlipRef txtSlipRef slipBank slipDate" event="change" />
                            </p:selectOneMenu>

                            <h:outputLabel id="lblCardRef" value="Card Ref. No" style="display: #{billController.paymentMethod ne 'Card' ? 'none' : 'block'} ; " />                                
                            <p:inputText autocomplete="off" id="txtCardRef"   value="#{billController.creditCardRefNo}" style="display: #{billController.paymentMethod ne 'Card' ? 'none' : 'block'} ; " />                                
                            <h:selectOneMenu id="creditBank" value="#{billController.creditBank}" style="display: #{billController.paymentMethod ne 'Card' ? 'none' : 'block'} ; ">
                                <f:selectItem itemLabel="Select Bank"/>
                                <f:selectItems value="#{institutionController.banks}" var="inst" itemLabel="#{inst.name}" itemValue="#{inst}"/>
                            </h:selectOneMenu>

                            <h:outputLabel id="lblCheqRef" value="Cheque No" style="display: #{billController.paymentMethod ne 'Cheque' ? 'none' : 'block'} ; " />
                            <p:inputText autocomplete="off"  id="txtCheqRef" value="#{billController.chequeRefNo}" style="display: #{billController.paymentMethod ne 'Cheque' ? 'none' : 'block'} ; " />
                            <h:selectOneMenu id="bank" value="#{billController.chequeBank}" style="display: #{billController.paymentMethod ne 'Cheque' ? 'none' : 'block'} ; ">
                                <f:selectItem itemLabel="Select Bank"/>
                                <f:selectItems value="#{institutionController.banks}" var="inst" itemLabel="#{inst.name}" itemValue="#{inst}"/>
                            </h:selectOneMenu>
                            <p:calendar id="chequeDate"  value="#{billController.chequeDate}" pattern="dd MMMM yyyy" style="display: #{billController.paymentMethod ne 'Cheque' ? 'none' : 'block'} ; ">                            
                            </p:calendar>

                            <h:outputLabel id="lblSlipRef" value="Slip Memo" style="display: #{billController.paymentMethod ne 'Slip' ? 'none' : 'block'} ; " />
                            <p:inputText autocomplete="off"  id="txtSlipRef" value="#{billController.comment}" style="display: #{billController.paymentMethod ne 'Slip' ? 'none' : 'block'} ; " />
                            <h:selectOneMenu id="slipBank" value="#{billController.slipBank}" style="display: #{billController.paymentMethod ne 'Slip' ? 'none' : 'block'} ; ">
                                <f:selectItem itemLabel="Select Bank"/>
                                <f:selectItems value="#{institutionController.banks}" var="inst" itemLabel="#{inst.name}" itemValue="#{inst}"/>
                            </h:selectOneMenu>

                            <p:calendar id="slipDate"  value="#{billController.slipDate}" pattern="dd MMMM yyyy" style="display: #{billController.paymentMethod ne 'Slip' ? 'none' : 'block'} ; ">                            
                            </p:calendar>

                            <p:commandButton  value="Settle" action="#{billController.settleBill}" ajax="false"  style="width: 150px; padding: 1px;border: 1px solid ; margin: auto;">
                            </p:commandButton>
                            <p:commandButton value="New Bill" ajax="false" action="#{billController.prepareNewBill()}" >
                            </p:commandButton>
                            <p:commandButton value="Mark as Foreigner" action="#{billController.markAsForeigner}" rendered="#{!billController.foreigner}" ajax="false" ></p:commandButton>
                            <p:commandButton value="Mark as Local" action="#{billController.markAsLocal}" rendered="#{billController.foreigner}" ajax="false" ></p:commandButton>

                            <p:watermark id="wcd" value="Cheque Date" for="chequeDate"/>
                            <p:watermark id="wsd" value="Slip Date" for="slipDate"/>
                        </h:panelGrid>



                        <h:panelGrid columns="2" >

                            <h:panelGrid columns="1" >

                                <h:panelGroup id="panelPatient" >

                                    <p:tabView id="tvPt" style="min-height: 250px;"  >
                                        <p:ajax event="tabChange"  process="@this" listener="#{billController.onTabChange}" />

                                        <p:tab id="tabNewPt" title="New Patient"  >
                                            <ez:newPatient controller="#{billController}"/>
                                        </p:tab>
                                        <p:tab id="tabSearchPt" title="Search Patient">
                                            <ez:searchedPatient controller="#{billController}"/>

                                        </p:tab>

                                    </p:tabView>

                                </h:panelGroup>

                                <h:panelGroup id="panelIx" >


                                    <p:panel id='pIxAdd' header="Items Requests" style="min-height: 350px;"  >
                                        <p:focus  context="acIx"/>
                                        <p:autoComplete widgetVar="aIx" id="acIx" forceSelection="true" value="#{billController.currentBillItem.item}" completeMethod="#{itemController.completeItemWithoutPack}" var="ix" itemLabel="#{ix.name}" itemValue="#{ix}" size="30"  style="width: 400px;" >
                                            <p:column>
                                                #{ix.name}
                                            </p:column>
                                            <p:column>
                                                #{ix.department.name}
                                            </p:column>
                                            <p:column>
                                                #{ix.total}
                                            </p:column>

                                        </p:autoComplete>
                                        <h:commandButton id="btnAddIx" value="Add" action="#{billController.addToBill()}">
                                            <f:ajax execute="acIx btnAddIx" render="focusIx tblBillItem  pBillDetails pIxAdd" />
                                        </h:commandButton>
                                        <p:defaultCommand target="btnAddIx" />
                                        <p:panel header="Session"  >
                                            <p:calendar id="calS" mode="inline" value="#{billController.sessionDate}" pattern="dd MMMM yyyy" >
                                                <f:ajax event="dateSelect" execute="calS" render="tblSs lblTemS" />
                                            </p:calendar>
                                            <h:outputLabel id="lblTemS" value="#{billController.sessionDate}" />
                                            <p:dataTable id="tblSs" value="#{billController.billSessions}" var="bs" emptyMessage="No Numbers Yet">
                                                <f:facet name="header">
                                                    <h:outputLabel value="#{billController.lastBillItem.item.name}" />
                                                </f:facet>
                                                <p:column>
                                                    <h:outputLabel value="#{bs.serialNo}" />
                                                </p:column>
                                                <p:column>
                                                    <h:outputLabel value="#{bs.bill.patient.person.name}" />
                                                </p:column>
                                            </p:dataTable>
                                        </p:panel>
                                    </p:panel>



                                </h:panelGroup>

                            </h:panelGrid>


                            <h:panelGrid columns="1" >

                                <p:dialog id="panDoc" header="Add New Doctor" widgetVar="dlg" resizable="false">  
                                    <h:panelGrid columns="2" style="margin-bottom:10px">  
                                        <h:outputText id="lblNameD" value="Name" ></h:outputText>
                                        <h:inputText autocomplete="off" id="txtNameD"  value="#{doctorController.current.person.name}"  ></h:inputText>
                                        <h:outputText id="lblPhoneD" value="Phone" ></h:outputText>
                                        <h:inputText autocomplete="off" id="txtPhoneD" value="#{doctorController.current.person.phone}"  ></h:inputText>
                                        <h:outputText id="lblFaxD" value="Fax" ></h:outputText>
                                        <h:inputText autocomplete="off" id="txtFaxD" value="#{doctorController.current.person.fax}"  ></h:inputText>
                                        <h:outputText id="lblMobileD" value="Mobile" ></h:outputText>
                                        <h:inputText autocomplete="off" id="txtMobileD" value="#{doctorController.current.person.mobile}"  ></h:inputText>
                                        <h:outputText id="lblAddressD" value="Address" ></h:outputText>
                                        <h:inputText autocomplete="off" id="txtAddressD" value="#{doctorController.current.person.address}"  ></h:inputText>
                                        <h:outputText value="Speciality" ></h:outputText>                                    
                                        <h:selectOneListbox id="cmbSpecialityD" size="1" value="#{doctorController.current.speciality}" >
                                            <f:selectItem itemLabel="Please select a speciality"/>
                                            <f:selectItems value="#{specialityController.items}" var="cat" itemLabel="#{cat.name}" itemValue="#{cat}" />
                                        </h:selectOneListbox>
                                        <h:outputText id="lblRegD" value="Registration" ></h:outputText>
                                        <h:inputText autocomplete="off" id="txtRegD" value="#{doctorController.current.registration}"  ></h:inputText>                 
                                        <h:outputText id="lblQuaD" value="Qualification" ></h:outputText>
                                        <h:inputText autocomplete="off" id="txtQuaD" value="#{doctorController.current.qualification}"  ></h:inputText>   

                                        <p:commandButton id="btnDocSave" value="Add New" process="btnDocSave panDoc" update="panDoc :#{p:component('cmbDoc')}" actionListener="#{doctorController.saveSelected()}" styleClass="buttons" oncomplete="dlg.hide();">
                                        </p:commandButton>

                                    </h:panelGrid>  

                                </p:dialog>  


                                <h:panelGroup id="panelBillReferrals" >
                                    <p:tabView >
                                        <p:tab title="Refering Doctor">
                                            <p:autoComplete forceSelection="true" id="cmbDoc" value="#{billController.referredBy}" completeMethod="#{doctorController.completeDoctor}" var="ix" itemLabel="#{ix.person.name}" itemValue="#{ix}" size="30"  style="width: 400px;">
                                            </p:autoComplete>
                                            <p:commandButton id="btnAddNewDoc" type="button" value="Add New Doctor" onclick="dlg.show();" actionListener="#{doctorController.prepareAdd}" process="btnAddNewDoc" update="#{p:component('panDoc')}" />  
                                        </p:tab>
                                        <p:tab title="Company" >
                                            <p:autoComplete id="creditCom" forceSelection="true" value="#{billController.creditCompany}"  completeMethod="#{creditCompanyController.completeCredit}" var="ix" itemLabel="#{ix.name}" itemValue="#{ix}" size="30"  style="width: 400px;">
                                                <f:ajax  event="itemSelect" listener="#{billController.calTotals()}" execute="@this" render=":#{p:component('panelBillTotals')}  :#{p:component('tblRequests')} :#{p:component('tblBillItem')}" />
                                            </p:autoComplete>                                           
                                        </p:tab>                                       
                                        <p:tab title="Staff">
                                            <p:autoComplete forceSelection="true" converter="stfcon" value="#{billController.staff}" completeMethod="#{staffController.completeStaff}" var="ix2" itemLabel="#{ix2.person.name}" itemValue="#{ix2}" size="30"  style="width: 400px;">
                                            </p:autoComplete>
                                        </p:tab>
                                    </p:tabView>
                                </h:panelGroup>

                                <h:panelGroup id="panelBillTotals" >
                                    <p:panel header="Bill Details" id="pBillDetails"  >
                                        <p:panelGrid columns="2" columnClasses="numberCol, textCol"  >
                                            <h:outputLabel value="Total" ></h:outputLabel>
                                            <h:outputLabel id="tot" value="#{billController.total}" >
                                                <f:convertNumber pattern="#,##0.00" />
                                            </h:outputLabel>
                                            <h:outputLabel value="Discount" ></h:outputLabel>
                                            <h:outputLabel id="dis" value="#{billController.discount}" >
                                                <f:convertNumber pattern="#,##0.00" />
                                            </h:outputLabel>
                                            <h:outputLabel value="Net Total" ></h:outputLabel>
                                            <h:outputLabel id="netTotal" value="#{billController.netTotal}" >
                                                <f:convertNumber pattern="#,##0.00" />
                                            </h:outputLabel>
                                            <h:outputLabel value="Tendered" ></h:outputLabel>
                                            <p:inputText autocomplete="off"  id="paid" value="#{billController.strTenderedValue}" >
                                                <p:ajax process="netTotal paid" update="balance" event="change"/>
                                            </p:inputText>
                                            <h:outputLabel  value="Balance " ></h:outputLabel>
                                            <h:outputLabel id="balance" value="#{billController.cashBalance}" >
                                                <f:convertNumber pattern="#,##0.00" />
                                            </h:outputLabel>
                                        </p:panelGrid>

                                    </p:panel>
                                </h:panelGroup>


                                <h:panelGroup id="panelBillItems" >
                                    <p:tabView id="tblBillItem" >
                                        <p:tab id="tabBillItems" title="Bill Items" >
                                            <p:dataTable id="tblRequests" rowIndexVar="rowIndex" value="#{billController.lstBillEntries}" var="bi" >

                                                <p:column>
                                                    <f:facet name="header">No</f:facet>
                                                        #{rowIndex+1}
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">Item</f:facet>
                                                        #{bi.billItem.item.name}
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">Fee</f:facet>
                                                    <h:outputLabel value="#{bi.billItem.netValue}">
                                                        <f:convertNumber pattern="#,##0.00" />
                                                    </h:outputLabel>
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">Department</f:facet>
                                                        #{bi.billItem.item.department.name}
                                                </p:column>

                                                <p:column>
                                                    <h:commandButton id="btnRemove" value="X" action="#{billController.removeBillItem}" >
                                                        <f:setPropertyActionListener  value="#{rowIndex}" target="#{billController.index}" />
                                                        <p:ajax  process="btnRemove" update=":#{p:component('tblBillItem')} :#{p:component('pBillDetails')}" />
                                                    </h:commandButton>
                                                </p:column>
                                            </p:dataTable>
                                        </p:tab>
                                        <p:tab id="tabBillIx" title="Tests" >
                                            <p:dataTable rowIndexVar="rowIndex" value="#{billController.lstBillComponents}" var="bix" >
                                                <p:column>
                                                    <f:facet name="header">No</f:facet>
                                                        #{rowIndex+1}
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">Name</f:facet>
                                                        #{bix.item.name}
                                                </p:column>

                                            </p:dataTable>
                                        </p:tab>

                                        <p:tab id="tabBillFee" title="Fees" >
                                            <p:dataTable rowIndexVar="rowIndex" value="#{billController.lstBillFees}" var="bif" >
                                                <p:column>
                                                    <f:facet name="header">Item Name</f:facet>
                                                    <h:outputLabel value="#{bif.billItem.item.name}" />
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">Fee</f:facet>
                                                    <h:inputText autocomplete="off" id="txtFeeVal" value="#{bif.feeValue}" disabled="#{!bif.billItem.item.userChangable}" >
                                                        <f:convertNumber pattern="#,##0.00" />
                                                        <f:ajax event="change" execute="txtFeeVal" render=" :#{p:component('tblRequests')}  :#{p:component('pBillDetails')}" listener="#{billController.feeChanged}" />
                                                    </h:inputText>
                                                </p:column>

                                                <p:column>
                                                    <f:facet name="header">Payee</f:facet>
                                                    <h:outputLabel value="#{bif.fee.name}" rendered="#{bif.staff eq null}" ></h:outputLabel>
                                                    <h:outputLabel value="#{bif.fee.speciality.name}" rendered="#{bif.staff!=null and bif.fee.speciality==null}" ></h:outputLabel>

                                                    <p:selectOneMenu value="#{bif.staff}" rendered="#{bif.fee.speciality!=null}" >
                                                        <f:selectItem itemLabel="Select Staff" />
                                                        <f:selectItems  value="#{staffController.getSpecialityStaff(bif.fee.speciality)}" var="bifs" itemLabel="#{bifs.person.name}" itemValue="#{bifs}" />
                                                    </p:selectOneMenu>

                                                </p:column>
                                            </p:dataTable>
                                        </p:tab>
                                    </p:tabView>
                                </h:panelGroup>





                            </h:panelGrid>


                        </h:panelGrid>
                    </p:panel>
                </h:panelGroup>

                <h:panelGroup rendered="#{billController.printPreview}" >
                    <div class="nonPrintBlock" >
                        <p:commandButton value="Print" ajax="false" action="#" >
                            <p:printer target="gpBillPreview" ></p:printer>
                        </p:commandButton>
                        <p:commandButton value="New Bill" ajax="false" action="#{billController.prepareNewBill()}" >
                        </p:commandButton>
                        <p:commandButton ajax="false" value="Batch Cancel" action="#{billController.cancellAll}" style="float: right;"
                                         onclick="if (!confirm('Are you sure you want to Cancel This Bill ?'))
                                                     return false;"/>
                    </div>
                    <div >


                        <h:panelGroup   id="gpBillPreview"   >

                            <div>

                                <ui:repeat value="#{billController.bills}" var="bill" >

                                    <div class="fiveinchbillBreak" >

                                        <div >


                                            <div class="receiptno" >
                                                <h:outputLabel value="#{bill.insId}">
                                                </h:outputLabel>
                                            </div>

                                            <div class="billdate" >
                                                <h:outputLabel value="#{bill.billDate}">
                                                    <f:convertDateTime pattern="dd/MM/yy"/>
                                                </h:outputLabel>
                                            </div>

                                            <div class="payment" >
                                                <h:outputLabel value="#{bill.paymentScheme.name}">
                                                    <f:convertDateTime pattern="dd/MM/yy"/>
                                                </h:outputLabel>
                                            </div>

                                            <div class="billtime" >
                                                <h:outputLabel value="#{bill.billTime}"
                                                               >
                                                    <f:convertDateTime pattern="hh mm a"/>
                                                </h:outputLabel>
                                            </div>

                                            <div class="ptname" >
                                                <h:outputLabel value="#{bill.patient.person.nameWithTitle}"
                                                               >
                                                </h:outputLabel>
                                            </div>

                                            <div class="phone" >
                                                <h:outputLabel value="#{bill.patient.person.phone}"
                                                               >
                                                </h:outputLabel>
                                            </div>

                                            <div class="cashier" >
                                                <h:outputLabel value="Cashier : #{bill.creater.code}">
                                                </h:outputLabel>
                                            </div>

                                            <div class="consult" >
                                                <h:outputLabel value="#{bill.collectingCentre.name}" />
                                                <h:outputLabel value="#{bill.creditCompany.name}" />
                                            </div>



                                            <div class="items" >
                                                <h:dataTable value="#{billController.lstBillEntries}" var="bip"   >
                                                    <h:column >
                                                        <h:outputLabel rendered="#{bip.billItem.item.department.id eq bill.toDepartment.id}" value="#{bip.billItem.item.printName}" style="margin: 4px; font-size: 11px!important;" >
                                                        </h:outputLabel>
                                                        <h:panelGroup rendered="#{bip.billItem.item.department.id eq bill.toDepartment.id and bip.billItem.billSession ne null }"  >
                                                            <br/>
                                                            <h:outputLabel value="Session No . " ></h:outputLabel>
                                                            <h:outputLabel value="#{bip.billItem.billSession.serialNo}" style="margin: 4px; font-size: 11px!important;" >
                                                            </h:outputLabel>
                                                        </h:panelGroup>
                                                    </h:column>
                                                </h:dataTable>
                                            </div>

                                            <div class="rates"  >
                                                <h:dataTable value="#{billController.lstBillEntries}" var="bip1"  >
                                                    <h:column >
                                                        <h:outputLabel  rendered="#{bip1.billItem.bill.id eq bill.id}"  value="#{bip1.billItem.grossValue}"  style="margin: 4px; font-size: 11px!important;" >
                                                            <f:convertNumber pattern="#,##0.00" />
                                                        </h:outputLabel>
                                                    </h:column>
                                                </h:dataTable>
                                            </div>

                                            <div class="totals" >

                                                <table>
                                                    <tr>
                                                        <td>
                                                            <br/>
                                                            <br/>
                                                        </td>
                                                        <td>
                                                            <br/>
                                                            <br/>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="text-align: left; min-width: 3cm;">
                                                            <h:outputLabel value="Total" style="  text-align: right!important;  font-size: 15px!important;   "/>
                                                        </td>
                                                        <td style="text-align: right!important; ">
                                                            <h:outputLabel value="#{bill.total}"  style=" font-weight: bold ; text-align: right!important; font-size: 15px!important;    ">
                                                                <f:convertNumber pattern="#,##0.00" />
                                                            </h:outputLabel>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="text-align: left;">
                                                            <h:outputLabel  rendered="#{bill.discount ne 0.0}" style=" text-align: right!important;  font-size: 15px!important;   "  value="Discount"/>
                                                        </td>
                                                        <td style="text-align: right!important; ">
                                                            <h:outputLabel rendered="#{bill.discount ne 0.0}"   value="#{bill.discount}"  style=" text-align: right!important;  font-size: 15px!important;   ">
                                                                <f:convertNumber pattern="#,##0.00" />
                                                            </h:outputLabel>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="text-align: left;">
                                                            <h:outputLabel  rendered="#{bill.discount ne 0.0}"    value="Net Total" style=" text-align: right!important;  font-size: 15px!important;   "/>
                                                        </td>
                                                        <td style="text-align: right!important; ">
                                                            <h:outputLabel  rendered="#{bill.discount ne 0.0}"    value="#{bill.netTotal}"  style="font-weight: bold ; text-align: right!important; font-size: 15px!important;    ">
                                                                <f:convertNumber pattern="#,##0.00" />
                                                            </h:outputLabel>
                                                        </td>
                                                    </tr>


                                                </table>

                                            </div>

                                        </div>

                                    </div>

                                </ui:repeat>

                            </div>


                        </h:panelGroup>




                    </div>
                </h:panelGroup>

            </h:form>

        </h:panelGroup>



    </h:body>
</html>

